<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\MessageRepository;
use App\Http\Servers\Admin\MessageServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class MessageController extends CommonController
{
    //留言设置
    public function field(Request $request, MessageServer $messageServer)
    {
        if ($request->has('status')) {
            if ($message = $messageServer->noPower()) {
                return back()->with($message);
            }
            $messageField = \App\MessageField::findorfail($request->status);
            $messageField['status'] = (bool)$messageField['status'] ? 0 : 1;
            $result = $messageField->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', '留言设置 ' . $messageField->name . ' 状态修改成功!');
            } else {
                $message['message'] = message(false, '修改失败', '留言设置 ' . $messageField->name . ' 状态修改失败!');
            }
            return back()->with($message);
        }

        if ($request->has('del')) {
            if ($message = $messageServer->noPower()) {
                return back()->with($message);
            }
            $messageField = \App\MessageField::findorfail($request->del);
            $result = $messageField->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', '留言设置 ' . $messageField->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', '留言设置 ' . $messageField->name . ' 删除失败!');
            }
            return back()->with($message);
        }

        $data['fields'] = \App\MessageField::orderBy('field')->get();

        return view('admin.message.field', $data);
    }

    //留言设置添加
    public function field_add(Request $request, MessageRepository $messageRepository, MessageServer $messageServer)
    {
        if ($request->isMethod('post')) {
            if ($message = $messageServer->noPower()) {
                return back()->with($message);
            }

            $this->validate($request, [
                'name' => 'required',
                'field' => 'required|unique:message_fields',
                'alias' => 'required|alpha_dash|unique:message_fields',
            ]);

            $create['name'] = $request->name;
            $create['field'] = $request->field;
            $create['alias'] = $request->alias;
            $create['value'] = $request->value;
            $create['verify'] = $request->verify;
            $create['status'] = $request->has('status') ? 1 : 0;
            $create['show'] = $request->has('show') ? 1 : 0;

            $result = \App\MessageField::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', $request->name . '设置成功,' . $request->field . '已与' . $request->alias . '绑定!');
            } else {
                $message['message'] = message(false, '添加失败', '添加在线留言设置失败!');
            }
            return back()->with($message);
        }

        $data['fields'] = $messageRepository->getNoBindField();
        if (count($data['fields']) <= 0) {
            $message['message'] = message(false, '添加失败', '所有字段都已绑定,无法添加新的绑定!');
            return back()->with($message);
        }

        return view('admin.message.field_add', $data);
    }

    //留言设置编辑
    public function field_edit(Request $request, MessageRepository $messageRepository, MessageServer $messageServer)
    {
        if ($request->isMethod('post')) {
            if ($message = $messageServer->noPower()) {
                return back()->with($message);
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'field' => 'required|unique:message_fields,field,' . $request->id . ',id',
                'alias' => 'required|alpha_dash|unique:message_fields,alias,' . $request->id . ',id',
            ]);

            $update['name'] = $request->name;
            $update['field'] = $request->field;
            $update['alias'] = $request->alias;
            $update['value'] = $request->value;
            $update['verify'] = $request->verify;
            $update['status'] = $request->has('status') ? 1 : 0;
            $update['show'] = $request->has('show') ? 1 : 0;

            $result = \App\MessageField::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', $request->name . '设置成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '编辑在线留言设置失败!');
            }
            return back()->with($message);
        }

        $data['field'] = \App\MessageField::findorfail($request->id);

        $data['fields'] = $messageRepository->getNoBindField();
        $data['fields'][] = $data['field']['field'];
        sort($data['fields']);

        return view('admin.message.field_edit', $data);
    }

    //在线留言列表
    public function message(Request $request, MessageRepository $messageRepository)
    {
        if ($request->has('del')) {
            $message = \App\Message::findorfail($request->del);
            $result = $message->delete();
            if ($result) {
                $m['message'] = message(true, '删除成功', '留言删除成功!');
            } else {
                $m['message'] = message(false, '删除失败', '留言删除失败!');
            }
            return back()->with($m);
        }

        if ($request->has('status')) {
            $message = \App\Message::findorfail($request->status);
            $message['status'] = (bool)$message['status'] ? 0 : 1;
            $result = $message->save();
            if ($result) {
                $m['message'] = message(true, '修改成功', '留言状态修改成功!');
            } else {
                $m['message'] = message(false, '修改失败', '留言状态修改失败!');
            }
            return back()->with($m);
        }

        $data['messages'] = $messageRepository->getMessagForAdmin();

        $data['messageLinkInfoNum'] = \App\Message::whereNotNull('info')->count();

        return view('admin.message.message', $data);
    }
}
